import {defineStore} from 'pinia'
import {store} from '@/store'
import {getRecommendSongList} from '@/api/recommend'
import {PlayList, SongList, Track} from '@/types'
import {getUserPlayList} from '@/api/user'
import {getPlaylistDetail} from "@/api/playlist.ts";

interface RecommendSongListStore {
    updateTime?: Date
    songLists: SongList[]
}

const useRecommendSongListStore = defineStore('recommendSongListStore', {
    state(): RecommendSongListStore {
        return {
            updateTime: undefined,
            songLists: []
        }
    },
    actions: {
        async fetchRecommendSongList() {
            const songList = await getRecommendSongList()
            this.songLists = songList.recommend
            this.updateTime = new Date()
        }
    },
    getters: {
        getRecommendSongList: (state: RecommendSongListStore) => state.songLists
    }
})

export function getRecommendSongListStore() {
    return useRecommendSongListStore(store)
}

interface MySongListStore {
    playlists: PlayList[]
    myLikeTracks: Set<Track['id']> | null
}

type MySongListStoreGetters = {
    getMyCreatedPlaylist(state: MySongListStore): PlayList[]
    getMyLikePlaylist(state: MySongListStore): PlayList
    getMySubscribedPlaylist(state: MySongListStore): PlayList[]
    computedIsLikeTrack(state: MySongListStore): (track: Track) => boolean
}
type MySongListStoreActions = {
    fetchMySongList(): Promise<void>
    clearMySongList(): Promise<void>
    fetchMyLikeTrack(): Promise<void>
}

const useMySongListStore = defineStore<'mySongListStore', MySongListStore, MySongListStoreGetters, MySongListStoreActions>('mySongListStore', {
    state() {
        return {
            playlists: [],
            myLikeTracks: null
        }
    },
    actions: {
        async fetchMySongList() {
            const {playlist} = await getUserPlayList()
            this.playlists = playlist
        },
        async clearMySongList() {
            this.playlists = []
        },
        async fetchMyLikeTrack() {
            if (this.playlists.length === 0) {
                await this.fetchMySongList()
            }
            const playlist = this.playlists[0];
            const {playlist: {tracks}} = await getPlaylistDetail(playlist.id);
            this.myLikeTracks = new Set(tracks.map(track => track.id))
        },
    },
    getters: {
        getMyCreatedPlaylist: (state: MySongListStore) => state.playlists.slice(1).filter(({subscribed}) => !subscribed),
        getMyLikePlaylist: (state: MySongListStore) => state.playlists.length > 0 ? state.playlists[0] : {},
        getMySubscribedPlaylist: (state: MySongListStore) => state.playlists.filter(({subscribed}) => subscribed),
        computedIsLikeTrack: (state: MySongListStore) => {
            if (state.myLikeTracks == null) {
                return (track: Track) => false
            }
            return (track: Track) => state.myLikeTracks.has(track.id)
        }
    },
    persist: {
        enabled: true
    }
})

export function getMySongListStore() {
    return useMySongListStore(store)
}
